package com.mysoft.gfyx.common.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class OssUtil {
    private static String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    private static String accessKeyId = "LTAI5tAkWjrrenAugNcuuWTA";
    private static String accessKeySecret = "4k7QKFjVqps3etGorMUl8LxczA0K0P";

    private static String bucketName = "gfyx-saas-bid-content";
    private static String key = "*** Provide key ***";

    public static String getOssFile(String fileName) throws IOException {
        OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        StringBuilder fileContent = new StringBuilder();
        try {
            // 调用ossClient.getObject返回一个OSSObject实例，该实例包含文件内容及文件元信息。
            OSSObject ossObject = client.getObject(bucketName, fileName);
            // 调用ossObject.getObjectContent获取文件输入流，可读取此输入流获取其内容。
            InputStream content = ossObject.getObjectContent();
            if (content != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                while (true) {
                    String line = reader.readLine();
                    if (line == null) break;
                    fileContent.append(line).append("\n");
                    //System.out.println("\n" + line);
                }
                // 数据读取完成后，获取的流必须关闭，否则会造成连接泄漏，导致请求无连接可用，程序无法正常工作。
                content.close();
                return fileContent.toString();
            }

        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
            System.out.println("Error Message: " + oe.getErrorMessage());
            System.out.println("Error Code:       " + oe.getErrorCode());
            System.out.println("Request ID:      " + oe.getRequestId());
            System.out.println("Host ID:           " + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
            System.out.println("Error Message: " + ce.getMessage());
        } finally {
            /*
             * Do not forget to shut down the client finally to release all allocated resources.
             */
            client.shutdown();
        }
        return fileContent.toString();
    }


    public static String getBidNoticeFilePath(String publishTime, String websiteName, String id) {
        //发布时间
        if (publishTime == null || publishTime.equals("")) {
            return "";
        }
        if (publishTime.length() >= 10) {
            publishTime = publishTime.substring(0, 10);
        }
        //站点
        if (websiteName == null) {
            assert websiteName != null;
            if (websiteName.equals("")) {
                websiteName = "other";
            }
        }
        return "pub_bid/" + publishTime + "/" + websiteName + "/" + id + ".txt";
    }

    public static String getWinBidFilePath(String publishTime, String websiteName, String id) {
        //发布时间
        if (publishTime == null || publishTime.equals("")) {
            return "";
        }
        if (publishTime.length() >= 10) {
            publishTime = publishTime.substring(0, 10);
        }
        //站点
        if (websiteName == null) {
            assert websiteName != null;
            if (websiteName.equals("")) {
                websiteName = "other";
            }
        }
        return "win_bid/" + publishTime + "/" + websiteName + "/" + id + ".txt";
    }
}
